package nl.hu.verbrandendephoenix.controller;

import java.util.ArrayList;

import nl.hu.verbrandendephoenix.client.AppManager;
import nl.hu.verbrandendephoenix.client.NotificationManager;
import nl.hu.verbrandendephoenix.shared.Conversation;
import nl.hu.verbrandendephoenix.shared.User;

import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Panel;

public class Inbox {
	private NotificationManager nM = NotificationManager.get();
	private nl.hu.verbrandendephoenix.view.Inbox view = new nl.hu.verbrandendephoenix.view.Inbox();
	private User u;
	
	public Inbox() {}
	
	
	public void initiate() {
		AppManager.get().getRpcService().getUserData(Cookies.getCookie("loginString"), new AsyncCallback<User>() {
			public void onFailure(Throwable caught) {
				nM.commError();
			}
			public void onSuccess(User result) {
				if(result != null) {
					u = result;
					setData();
					addHandlers();
				} else {
					nM.SID_Invalid();
				}
			}
		});
	}
	
	public Panel getPanel() {
		Panel p = view.get();
		initiate();
		return p;
	}
	
	private void setData() {
		AppManager.get().getRpcService().getConversations(u, new AsyncCallback<ArrayList<Conversation>>() {
			public void onFailure(Throwable caught) {
				System.out.println("Faalhaas");
				nM.commError();
			}

			@Override
			public void onSuccess(ArrayList<Conversation> result) {
				setConversationList(result);
			}
		});
	}
	
	private void processSubmit() {
		
	}
	
	public void addHandlers() {

	}

	private void setConversationList(ArrayList<Conversation> conv) {
		int i = 0;
		for(Conversation conver : conv) {
			view.addConv(i, conver);
			i++;
		}
	}
	
}
